package com.taotao.cart.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.taotao.cart.bean.TbUser;
import com.taotao.cart.pojo.Cart;
import com.taotao.cart.service.CartCookieService;
import com.taotao.cart.service.CartService;
import com.taotao.cart.threadlocal.UserThreadLocal;

@Controller
@RequestMapping("cart")
public class CartController {
	
	@Autowired
	private CartService cartService;
	
	@Autowired
	private CartCookieService cartCookieService;

	@RequestMapping(value = "list",method = RequestMethod.GET)
	public ModelAndView cartList( HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("cart");
		TbUser user = UserThreadLocal.get();
		List<Cart> carts = null;
		if(null == user) {
			//查询cookie
			carts = this.cartCookieService.queryCartList(request);
		}else {
			carts = this.cartService.queryCartList();
			//查询数据库
		}
		modelAndView.addObject("cartList", carts);
		return modelAndView;
	}
	
	/**
	 * 
	 * <p>Title: 将商品添加到购物车中</p>
	 * <p>Description: 如果用户未登录，则将购物车放在cookie中，如果登录则放到数据库中</p>
	 * @param itemId
	 * @return
	 */
	@RequestMapping(value="{itemId}",method = RequestMethod.GET)
	public String addToCart(@PathVariable("itemId") Long itemId, HttpServletRequest request,
            HttpServletResponse response) {
		TbUser user = UserThreadLocal.get();
		if(null == user) {
			  // 未登录状态
            this.cartCookieService.addItemToCart(itemId, request, response);
		}else {
			this.cartService.addItemToCart(itemId, 1);
		}
		return "redirect:list.html";
	}
	
	 /**
     * 更新购买商品的数量
     * 
     * @param itemId
     * @param num 最终购买的商品数量
     * @return
     */
    @RequestMapping(value = "update/num/{itemId}/{num}", method = RequestMethod.POST)
    public ResponseEntity<Void> udpateNum(@PathVariable("itemId") Long itemId,
            @PathVariable("num") Integer num, HttpServletRequest request, HttpServletResponse response) {
        // 判断用户是否登录
    	TbUser user = UserThreadLocal.get();
        if (user == null) {
            // 未登录状态
            this.cartCookieService.updateNum(itemId, num, request, response);
        } else {
            // 已登录
            this.cartService.updateNum(itemId, num);
        }
        return ResponseEntity.ok(null);
    }

    /**
     * 删除购物车中的商品
     * 
     * @param itemId
     * @return
     */
    @RequestMapping(value = "delete/{itemId}", method = RequestMethod.GET)
    public String deleteItem(@PathVariable("itemId") Long itemId, HttpServletRequest request,
            HttpServletResponse response) {
        // 判断用户是否登录
        TbUser user = UserThreadLocal.get();
        if (user == null) {
            // 未登录状态
            this.cartCookieService.deleteItem(itemId, request, response);
        } else {
            // 已登录
            this.cartService.deleteItem(itemId);
        }
        return "redirect:/cart/list.html";
    }
}
